<?php

namespace App\Models\Cashbook;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;

class CashbookBillSyncSwitchModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;

    protected $table = "cashbook_bill_sync_switch";

    protected $fillable = [
        'id',
        'shopId',
        'type',
        'switch',
        'isDeleted',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];

    const TYPE_GOODS = 1;
    const TYPE_POS   = 2;

    const TYPE_TEXT_MAPPING = [
        self::TYPE_GOODS => '自动同步订货账单',    //自动同步/停止同步「夸父商家-订货」功能产生的账单
        self::TYPE_POS   => '自动同步每日营业额',   //自动同步/停止同步「夸父收银」记录的每日收益
    ];

    const SWITCH_OFF        = 0;
    const SWITCH_ON         = 1;

    const SWITCH_TEXT_MAPPING = [
        self::SWITCH_OFF => '关',
        self::SWITCH_ON  => '开',
    ];

    public static function insertData(array $data)
    {
        return self::query()->create($data);
    }

    public static function getSwitchData($shopId)
    {
        return self::query()
            ->where('shopId', $shopId)
            ->get()
            ->map(
                function ($switch) {
                    return [
                        'id'       => $switch['id'],
                        'type'     => $switch['type'],
                        'typeText' => CashbookBillSyncSwitchModel::TYPE_TEXT_MAPPING[$switch['type']] ?? "",
                        'switch'   => $switch['switch'],
                    ];
                }
            );
    }
}
